﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///BookInfo 的摘要说明
/// </summary>
/// 
public partial class BookInfo
{
    DataTableDataContext db = new DataTableDataContext();
   
    //根据ID查找图书
    public IEnumerable<BookInfo> GetBookNameById(long id)
    {
        var book = from b in db.BookInfo where b.bookId==id select b;
        return book;
    }


    //根据关键词查找图书
    public IEnumerable<BookInfo> GetBookByKeyWord(string word)
    {
        var book = (from b in db.BookInfo where b.bookName.Contains(word) select b).ToList();
        return book;
    }

    //根据作者姓名查找图书
    public IEnumerable<BookInfo> GetBookByAuthor(string authorName)
    {
        var book = (from b in db.BookInfo where b.author.Contains(authorName) select b).ToList();
        return book;
    }

    //根据小类型ID查找图书
    public IEnumerable<BookInfo> GetBookBySubType(int id)
    {
        var book =( from b in db.BookInfo where b.bookTypeId == id select b).ToList();
        return book;
 
    }


    //根据大类型ID查找图书
    public IEnumerable<BookInfo> GetBookBySuperType(int id)
    {
        var books = (from b in db.BookInfo
                    from sub in db.SubType
                    from s in db.SuperType
                    where b.bookTypeId == sub.subTypeId && sub.superTypeId == s.superTypeId && s.superTypeId==id
                    select b).ToList();
        return books;

        
    }

    
    //按价格升序排列
    public IEnumerable<BookInfo> AscendingOrderByPrice()
    {
        var book = from b in db.BookInfo orderby b.marketPrice ascending select b;
        return book;
    }

    //按点击量升序排列
    public IEnumerable<BookInfo> AscendingOrderByHitNum()
    {
        var book = from b in db.BookInfo orderby b.hitNum ascending select b;
        return book;
    }


    //按购买量升序排列
    public IEnumerable<BookInfo> AscendingOrderByBuyNum()
    {
        var book = from b in db.BookInfo orderby b.buyNum ascending select b;
        return book;
    }

    //增加图图书
    public void InsertBook(string mBookName, string mBookTypeId, string authorName, string publisherName, 
                           string ISBNName, String money, String inventoryNumber,string text,
                           string smallImgPath,string bigImgPath)
    {
        BookInfo book = new BookInfo 
        { 
            bookName = mBookName,
            bookTypeId=Convert.ToInt32(mBookTypeId),
            author=authorName,                                       
            publisher=publisherName,
            ISBN=ISBNName,
            marketPrice=Convert.ToDecimal(money),
            inventory= Convert.ToInt32(inventoryNumber),
            introduce=text,
            smallImg=smallImgPath,
            bigImg=bigImgPath
        };
        db.BookInfo.InsertOnSubmit(book);
        db.SubmitChanges();

    }

    //增加图书并返回其ID
    //public int InsertBookReturnId(string mBookName, string mBookTypeId, string authorName, string publisherName,
    //                              string ISBNName, String money, String inventoryNumber, string text,
    //                              string smallImgPath, string bigImgPath)
    //{
    //    InsertBook(mBookName, mBookTypeId, authorName, publisherName, ISBNName, money, inventoryNumber, text, smallImgPath, bigImgPath);
    //}
    public long InsertBookReturnId(string mBookName, string mBookTypeId, string authorName, string publisherName,
                       string ISBNName, String money, String inventoryNumber, string text,
                       string smallImgPath, string bigImgPath)
    {
        BookInfo book = new BookInfo
        {
            bookName = mBookName,
            bookTypeId = Convert.ToInt32(mBookTypeId),
            author = authorName,
            publisher = publisherName,
            ISBN = ISBNName,
            marketPrice = Convert.ToDecimal(money),
            inventory = Convert.ToInt32(inventoryNumber),
            introduce = text,
            smallImg = smallImgPath,
            bigImg = bigImgPath
        };
        db.BookInfo.InsertOnSubmit(book);
        db.SubmitChanges();
        long id=book.bookId ;
        return id;
    }

    //删除图书
    public void deleteBook(long id)
    {
        var books = from b in db.BookInfo where b.bookId == id select b;
        var recommendBooks = from r in db.RecommendBook from b in db.BookInfo where b.bookId == id && b.bookId == r.bookId select r;
       
        var comments = from r in db.Comment from b in db.BookInfo where b.bookId == id && b.bookId == r.bookId select r;
        var orderDetails = from r in db.OrderDetail from b in db.BookInfo where b.bookId == id && b.bookId == r.productId select r;
        foreach (var book in recommendBooks)
        {
            db.RecommendBook.DeleteOnSubmit(book);
            db.SubmitChanges();
        }
        
        foreach (var comment in comments)
        {
            db.Comment.DeleteOnSubmit(comment);
            db.SubmitChanges();
        }
        foreach (var detail in orderDetails)
        {
            db.OrderDetail.DeleteOnSubmit(detail);
            db.SubmitChanges();
 
        }
        foreach (var book in books)
        {
            db.BookInfo.DeleteOnSubmit(book);
            db.SubmitChanges();
        }
    }
    //修改公共折扣
    public void updatePublicDiscount(short  count)
    {
        var books = from b in db.BookInfo select b;
        foreach (var book in books)
        {
            book.publicDiscount = count;
        }
        db.SubmitChanges();
    }
    //增加点击率
    public void increaseHitNum(int id)
    {
        var books = from b in db.BookInfo where b.bookId == id select b;
        foreach (var book in books)
        {
            book.hitNum++;
            
        }
        db.SubmitChanges();
 
    }


    //增加购买量
    public void increaseBuyNum(int id)
    {
        var books = from b in db.BookInfo where b.bookId == id select b;
        foreach (var book in books)
        {
            book.buyNum++;
        }
        db.SubmitChanges();

    }
    //减少库存量
    public void decreaseInventory(long id)
    {
        var books = from b in db.BookInfo where b.bookId == id select b;
        foreach (var book in books)
        {
            book.inventory--;
        }
        db.SubmitChanges();
    }

    //启用公共折扣
    public void enabledPublicDiscount(bool isDiscount=false)
    {
        var books = from b in db.BookInfo select b;
        foreach (var book in books)
        {
            book.isDiscountForMember = isDiscount;
        }
        db.SubmitChanges();
    }


}